package com.nondo.dean.auth.service.impl;

import com.nondo.dean.auth.domain.RoleAuthorityExample;
import com.nondo.dean.auth.domain.RoleAuthorityKey;
import com.nondo.dean.auth.mapper.RoleAuthorityMapper;
import com.nondo.dean.auth.service.RoleAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author linfeng
 */
@Service
public class RoleAuthorityServiceImpl implements RoleAuthorityService {

    @Autowired
    private RoleAuthorityMapper roleAuthorityMapper;

    @Override
    public List<RoleAuthorityKey> findByRoleId(Long roleId) {

        RoleAuthorityExample example = new RoleAuthorityExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        return roleAuthorityMapper.selectByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRoleAuthority(RoleAuthorityKey key) {
        RoleAuthorityKey oldKey = findByRoleIdAndAuthorityId(key.getRoleId(), key.getAuthorityId());
        if (oldKey == null) {
            roleAuthorityMapper.insertSelective(key);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRoleAuthority(Long roleId, Long authorityId) {
        RoleAuthorityKey key = findByRoleIdAndAuthorityId(roleId, authorityId);
        if (key == null) {
            key = new RoleAuthorityKey();
            key.setRoleId(roleId);
            key.setAuthorityId(authorityId);
            roleAuthorityMapper.insertSelective(key);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoleAuthority(Long roleId, Long authorityId) {
        RoleAuthorityExample example = new RoleAuthorityExample();
        example.createCriteria().andRoleIdEqualTo(roleId).andAuthorityIdEqualTo(authorityId);
        roleAuthorityMapper.deleteByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoleAuthority(Long roleId) {
        RoleAuthorityExample example = new RoleAuthorityExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleAuthorityMapper.deleteByExample(example);
    }

    @Override
    public void deleteRoleAuthorityByAuthorityId(Long authorityId) {
        RoleAuthorityExample example = new RoleAuthorityExample();
        example.createCriteria().andAuthorityIdEqualTo(authorityId);
        roleAuthorityMapper.deleteByExample(example);
    }

    @Override
    public RoleAuthorityKey findByRoleIdAndAuthorityId(Long roleId, Long authorityId) {

        RoleAuthorityExample example = new RoleAuthorityExample();
        example.createCriteria().andRoleIdEqualTo(roleId).andAuthorityIdEqualTo(authorityId);
        List<RoleAuthorityKey> keys = roleAuthorityMapper.selectByExample(example);
        if (keys == null || keys.isEmpty()) {
            return null;
        }
        return keys.get(0);
    }


}
